/*
* $Id: InspectManagerTest.cpp 150 2010-09-22 19:09:10Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Components/Journal/InspectManager.h"
#include "Sources/Database/QueryResult.h"
#include <cppunit/TestCase.h>
#include<cppunit/extensions/HelperMacros.h>

class CInspectManagerTest : public CppUnit::TestCase
{
	CPPUNIT_TEST_SUITE(CInspectManagerTest);
	CPPUNIT_TEST(InitTest);
	CPPUNIT_TEST(AddTest);
	CPPUNIT_TEST(ChangeTest);
	CPPUNIT_TEST_SUITE_END();
private:
	std::vector<CTableItem> GetRowJournal(const std::tstring& strId,
		const std::tstring& strIdFamily, const std::tstring& strFrame,
		const std::tstring& strStrength, const std::tstring& strDate,
		const std::tstring& strQBState,	const std::tstring& strWax,
		const std::tstring& strAmbrosia, const std::tstring& strEvacuation,
		const std::tstring& strDieting, const std::tstring& strDesease,
		const std::tstring& strNote, const std::tstring& strIdRecord);
	std::vector<CTableItem> GetRowInspect(const std::tstring& strIdJournal,
		const std::tstring& strIdHousing, const std::tstring& strNameHousing,
		const std::tstring& strHoney, const std::tstring& strBrood,
		const std::tstring& strGoodHoney,	const std::tstring& strOpenBrood,
		const std::tstring& strCloseBrood);
	void Init(CInspectManager* pManager);
public:
	CInspectManagerTest(){}
	void InitTest();
	void AddTest();
	void ChangeTest();
};

std::vector<CTableItem> CInspectManagerTest::GetRowJournal(const std::tstring& strId,
	const std::tstring& strIdFamily, const std::tstring& strFrame, const std::tstring& strStrength,
	const std::tstring& strDate, const std::tstring& strQBState, const std::tstring& strWax,
	const std::tstring& strAmbrosia, const std::tstring& strEvacuation, const std::tstring& strDieting,
	const std::tstring& strDesease,	const std::tstring& strNote, const std::tstring& strIdRecord){
		std::vector<CTableItem> m_aItems;
		CTableItem item = {_T("id_journal"), strId};
		m_aItems.push_back(item);
		CTableItem item1 = {_T("frame"), strFrame};
		m_aItems.push_back(item1);
		CTableItem item2 = {_T("strength"), strStrength};
		m_aItems.push_back(item2);
		CTableItem item3 = {_T("date"), strDate};
		m_aItems.push_back(item3);
		CTableItem item4 = {_T("is_queen_bee"), strQBState};
		m_aItems.push_back(item4);
		CTableItem item5 = {_T("wax"), strWax};
		m_aItems.push_back(item5);
		CTableItem item6 = {_T("ambrosia"), strAmbrosia};
		m_aItems.push_back(item6);
		CTableItem item7 = {_T("evacuation"), strEvacuation};
		m_aItems.push_back(item7);
		CTableItem item8 = {_T("dieting"), strDieting};
		m_aItems.push_back(item8);
		CTableItem item9 = {_T("disease"), strDesease};
		m_aItems.push_back(item9);
		CTableItem item10 = {_T("note"), strNote};
		m_aItems.push_back(item10);
		CTableItem item11 = {_T("id_record"), strIdRecord};
		m_aItems.push_back(item11);
		CTableItem item12 = {_T("id_family"), strIdFamily};
		m_aItems.push_back(item12);
		return m_aItems;
}
std::vector<CTableItem> CInspectManagerTest::GetRowInspect(const std::tstring& strIdJournal,
	const std::tstring& strIdHousing, const std::tstring& strNameHousing, const std::tstring& strHoney,
	const std::tstring& strBrood, const std::tstring& strGoodHoney, const std::tstring& strOpenBrood,
	const std::tstring& strCloseBrood){
		std::vector<CTableItem> m_aItems;
		CTableItem item = {_T("id_journal"), strIdJournal};
		m_aItems.push_back(item);
		CTableItem item1 = {_T("name"), strNameHousing};
		m_aItems.push_back(item1);
		CTableItem item2 = {_T("honey"), strHoney};
		m_aItems.push_back(item2);
		CTableItem item3 = {_T("brood"), strBrood};
		m_aItems.push_back(item3);
		CTableItem item4 = {_T("open_brood"), strOpenBrood};
		m_aItems.push_back(item4);
		CTableItem item5 = {_T("close_brood"), strCloseBrood};
		m_aItems.push_back(item5);
		CTableItem item6 = {_T("goods_honey"), strGoodHoney};
		m_aItems.push_back(item6);
		CTableItem item7 = {_T("id_housing"), strIdHousing};
		m_aItems.push_back(item7);
		return m_aItems;
}


void CInspectManagerTest::Init(CInspectManager* pManager){
	MyTable aTable;
	g_QR.Init();
	aTable.push_back(GetRowJournal(_T("1"),_T("1"),_T("10"),_T("5"),_T("17.06.2010 01:00:00"),
		_T("0"),_T("0"),_T("0"),_T("0"), _T("0"), _T("0"), _T("descr"), _T("0")));
	aTable.push_back(GetRowJournal(_T("2"),_T("1"),_T("10"),_T("6"),_T("11.06.2010 01:00:00"),
		_T("0"),_T("0"),_T("0"),_T("0"), _T("0"), _T("0"), _T("descr"), _T("0")));
	aTable.push_back(GetRowJournal(_T("3"),_T("1"),_T("10"),_T("7"),_T("12.06.2010 01:00:00"),
		_T("0"),_T("0"),_T("0"),_T("0"), _T("0"), _T("0"), _T("descr"), _T("0")));
	aTable.push_back(GetRowJournal(_T("4"),_T("1"),_T("10"),_T("8"),_T("13.06.2010 01:00:00"),
		_T("0"),_T("0"),_T("0"),_T("0"), _T("0"), _T("0"), _T("descr"), _T("0")));
	aTable.push_back(GetRowJournal(_T("5"),_T("2"),_T("10"),_T("9"),_T("14.06.2010 01:00:00"),
		_T("0"),_T("0"),_T("0"),_T("0"), _T("0"), _T("0"), _T("descr"), _T("0")));
	aTable.push_back(GetRowJournal(_T("6"),_T("2"),_T("10"),_T("10"),_T("15.06.2010 01:00:00"),
		_T("0"),_T("0"),_T("0"),_T("0"), _T("0"), _T("0"), _T("descr"), _T("0")));
	g_QR.AddResult(aTable);
	aTable.clear();
	aTable.push_back(GetRowInspect(_T("1"),_T("1"),_T("1a"),_T("5"),_T("4"), _T("0"),_T("0"),_T("0")));
	aTable.push_back(GetRowInspect(_T("1"),_T("2"),_T("2a"),_T("0"),_T("4"), _T("0"),_T("0"),_T("0")));
	aTable.push_back(GetRowInspect(_T("2"),_T("1"),_T("1a"),_T("7"),_T("3"), _T("0"),_T("0"),_T("0")));
	aTable.push_back(GetRowInspect(_T("3"),_T("1"),_T("1a"),_T("7"),_T("4"), _T("0"),_T("0"),_T("0")));
	aTable.push_back(GetRowInspect(_T("4"),_T("1"),_T("1a"),_T("4"),_T("4"), _T("0"),_T("0"),_T("0")));
	aTable.push_back(GetRowInspect(_T("5"),_T("3"),_T("3a"),_T("5"),_T("4"), _T("0"),_T("0"),_T("0")));
	aTable.push_back(GetRowInspect(_T("6"),_T("3"),_T("3a"),_T("3"),_T("4"), _T("0"),_T("0"),_T("0")));
	g_QR.AddResult(aTable);
	pManager->Init();
}

void CInspectManagerTest::InitTest(){
	CInspectManager manager;
	Init(&manager);

	std::map<int, CInspectManager::journal> aJournal = manager.GetFamilyJournal(1);
	CPPUNIT_ASSERT(aJournal.size() == 4);
	std::vector<CInspectManager::inspect> aInspects = manager.GetInspects(1);
	CPPUNIT_ASSERT(aInspects.size() == 2);
	aInspects = manager.GetInspects(6);
	CPPUNIT_ASSERT(aInspects.size() == 1);
	CInspectManager::lastInspect li = manager.GetFamilyLastJournal(1);
	CPPUNIT_ASSERT(li.m_nHousing == 2);
	CPPUNIT_ASSERT(li.m_nStrength == 5);
	CPPUNIT_ASSERT(li.m_nBrood == 8);
	CPPUNIT_ASSERT(li.m_nHoney == 5);
}

void CInspectManagerTest::AddTest(){
	CInspectManager manager;
	Init(&manager);

	MyTable aTable;
	std::vector<CTableItem> aItems;
	CTableItem item = {_T("id_journal"), _T("7")};
	aItems.push_back(item);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);

	CInspectManager::journal jrn;
	time(&jrn.m_date);
	jrn.m_nFrame = 10;
	jrn.m_nIdFamily = 2;
	jrn.m_nStrength = 9;
	jrn.m_strNote = _T("descr");
	CInspectManager::inspect ins;
	ins.m_nHoney = 4;
	ins.m_nBrood = 7;
	std::vector<CInspectManager::inspect> aInspects;
	aInspects.push_back(ins);
	manager.AddInspect(jrn, aInspects, std::map<std::tstring, std::tstring>());
	aInspects = manager.GetInspects(7);
	CPPUNIT_ASSERT(aInspects.size() == 1);
	std::map<int, CInspectManager::journal> aJournals = manager.GetFamilyJournal(2);
	CPPUNIT_ASSERT(aJournals.size() == 3);
	CInspectManager::lastInspect li = manager.GetFamilyLastJournal(2);
	CPPUNIT_ASSERT(li.m_nIdJournal == 7);
}

void CInspectManagerTest::ChangeTest(){
	CInspectManager manager;
	Init(&manager);
	MyTable aTable;
	std::vector<CTableItem> aItems;
	CTableItem item = {_T("id_record"), _T("0")};
	aItems.push_back(item);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);

	CInspectManager::journal jrn;
	time(&jrn.m_date);
	jrn.m_nFrame = 10;
	jrn.m_nIdFamily = 1;
	jrn.m_nStrength = 9;
	CInspectManager::inspect ins;
	ins.m_nHoney = 4;
	ins.m_nBrood = 7;
	std::vector<CInspectManager::inspect> aInspects;
	aInspects.push_back(ins);
	manager.ChangeInspect(2, jrn, aInspects, std::map<std::tstring, std::tstring>());
	aInspects = manager.GetInspects(2);
	CPPUNIT_ASSERT(aInspects.size() == 1);
	std::map<int, CInspectManager::journal> aJournals = manager.GetFamilyJournal(1);
	CPPUNIT_ASSERT(aJournals.size() == 4);
	CInspectManager::lastInspect li = manager.GetFamilyLastJournal(1);
	CPPUNIT_ASSERT(li.m_nIdJournal == 2);
}

CPPUNIT_TEST_SUITE_REGISTRATION(CInspectManagerTest);